const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

// Page-服务项目
router.get('/service/price', async ctx => {
  let script = ['/servicePrice.list.js'];
  let modConfig = modules['servicePrice.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/service/price', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务项目', body, script, modConfig.template, ctx)
});

// Page-服务项目记录
router.get('/service/price/record', async ctx => {
  let script = ['/servicePrice.record.js'];
  let modConfig = modules['servicePrice.record'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/service/price/record', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务项目记录', body, script, modConfig.template, ctx)
});

// Page-服务项目-新建/编辑
router.get('/service/price/record', async ctx => {
  let script = ['/servicePrice.edit.js'];
  let modConfig = modules['servicePrice.edit'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/service/price/record', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
});

// Page-服务项目-新建/编辑
router
  .get('/service/price/createView/0', async ctx => {
    await servicePriceEditContent(ctx);
  })
  .get('/service/price/edit/:id', async ctx => {
    await servicePriceEditContent(ctx);
  })

// Page-服务项目-详情
router.get('/service/price/view/:id', async ctx => {
  let script = ['/servicePrice.view.js'];
  let modConfig = modules['servicePrice.view'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(`/service/price/view/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务项目详情', body, script, modConfig.template, ctx)
});


async function servicePriceEditContent(ctx) {
  let script = ['/servicePrice.edit.js'];
  let modConfig = modules['servicePrice.edit'];
  
  let { url, headers} = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
}

module.exports = router;